<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="采购日期">
                <a-date-picker format="YYYY-MM-DD" v-model="queryParam.purchaseDate" @change="changeDate" :show-today="true"/>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="供应商">
                <a-select placeholder="请选择供应商" v-model="queryParam.supplierCode" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in supplierOptions" :key="index" :value="d.supplierCode">{{ d.supplierName }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="审核状态">
                <a-select placeholder="请选择状态" v-model="queryParam.status" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 新增/修改 -->
      <biz-purchase-add-form v-if="showAddModal" ref="bizPurchaseAddForm"  :supplierOptions="supplierOptions" :warehouseIdOptions="warehouseIdOptions" :payOptions="payOptions" @ok="getList" @close="showAddModal = false" />
       <!-- 查看 -->
       <biz-purchase-view-form v-if="showViewModal" ref="bizPurchaseViewForm"  :supplierOptions="supplierOptions" :warehouseIdOptions="warehouseIdOptions" :payOptions="payOptions" @ok="getList" @close="showViewModal = false" />
      <!-- 付款 -->
      <biz-purchase-pay-form v-if="showPayModal" ref="bizPurchasePayForm" :supplierOptions="supplierOptions" :warehouseIdOptions="warehouseIdOptions" :payOptions="payOptions"  @ok="getList" @close="showPayModal = false" />
      <advance-table 
        :customRow="customRow"
        :scroll="{x:true}"
        :components="drag(columns)"
        title="采购单" :pagination="{
        current: queryParam.pageNum,
        pageSize: queryParam.pageSize,
        total: total,
        showSizeChanger: true,
        showLessItems: true,
        showQuickJumper: true,
        showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
        onChange: changeSize,
        onShowSizeChange: onShowSizeChange
      }" tableKey="base-bizOrder-index-table" @change="handleTableChange" rowKey="id" @refresh="getList"
      
        :columns="columns" :data-source="dataList" :loading="loading" :format-conditions="true">
        <div class="table-operations" slot="button">
          <a-button type="primary" @click="handleAdd({})" v-hasPermi="['biz:bizPurchase:add']">
            <a-icon type="plus" />新增
          </a-button>
        </div>
        <span slot="statusRender" slot-scope="{record}">
          {{ statusFormat(record) }}
        </span>
        <span slot="operation" slot-scope="{record}">
          <!-- <a @click="handleUpdate(record)" v-if="record.status == 1" v-hasPermi="['biz:bizPurchase:edit']">
            修改
          </a>
          <a @click="audit(record,2)" v-if="record.status == 1" v-hasPermi="['biz:bizPurchase:audit']">
            审核
          </a> -->
          <a @click="handleView(record)">
            查看
          </a>
          <a @click="handlePay(record)" v-if="record.status == 2" v-hasPermi="['biz:bizPayable:add']">
            付款
          </a>
          <!-- <a @click="handleDelete(record)" v-if="record.status == 1" v-hasPermi="['biz:bizPurchase:remove']">
            删除
          </a> -->
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { getInitData, listBizPurchase, auditPurchase,delBizPurchase } from '@/api/biz/bizPurchase'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import BizPurchaseAddForm from '@/views/biz/bizpurchase/modules/BizPurchaseAddForm'
import BizPurchaseViewForm from '@/views/biz/bizpurchase/modules/BizPurchaseViewForm'
import BizPurchasePayForm from '@/views/biz/bizpurchase/modules/BizPurchasePayForm'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import SelectDept from '@/components/pt/selectDept/SelectDept'
import { listAllBizSupplier } from '@/api/biz/bizSupplier'
import { allListBizWarehouse } from '@/api/biz/bizWarehouse'
import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
export default {
  name: 'BizPurchase',
  components: {
    AdvanceTable,
    BizPurchaseAddForm,
    BizPurchaseViewForm,
    BizPurchasePayForm,
    SelectUser,
    SelectDept
  },
  mixins:[tableDragResizeMixin],
  data() {
    return {
      showAddModal: false,
      showViewModal: false,
      showPayModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      statusOptions: [],
      payOptions: [],
      // 表格数据
      dataList: [],
      // 仓库字典
      warehouseIdOptions: [],
      // 供应商列表
      supplierOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        purchaseDate: undefined,
        supplierCode: undefined,
        status: undefined
      },
      columns: [
      {
          title: '采购日期',
          dataIndex:'purchaseDate',
          align: 'center'
        },
        {
          title: '采购单编号',
          dataIndex:'purchaseCode',
          align: 'center'
        },
        {
          title: '供应商名称',
          dataIndex:'supplierName',
          align: 'center'
        },
        {
          title: '仓库',
          dataIndex:'warehouseName',
          align: 'center'
        },
        {
          title: '操作员',
          dataIndex:'createByName',
          align: 'center'
        },
        {
          title: '部门',
          dataIndex:'deptName',
          align: 'center',
        },
        {
          title: '采购金额',
          dataIndex:'purchaseAmount',
          align: 'center'
        },
        {
          title: '付款金额',
          dataIndex:'payAmount',
          align: 'center'
        },
        {
          title: '审核状态',
          dataIndex:'status',
          align: 'center',
          scopedSlots: { customRender: 'statusRender' },
        },
        {
          title: '备注',
          dataIndex:'remark',
          align: 'center'
        },
        {
          title: '操作',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created() {
    this.getList()
    getInitData('purchase_status,pay_type').then(response => {
      this.statusOptions = response.data.purchase_status
      this.payOptions = response.data.pay_type
    })
    listAllBizSupplier().then(response => {
      this.supplierOptions = response.data;
    })
    allListBizWarehouse().then(response => {
      this.warehouseIdOptions = response.data
    })
  },
  methods: {
    /** 查询销售单列表 */
    getList() {
      this.loading = true
      listBizPurchase(this.queryParam).then(response => {
        this.dataList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    audit(row,status){
      var that = this
      this.$confirm({
        title: '确认审核所选中数据?',
        onOk () {
          return auditPurchase(row.id,status)
            .then(() => {
              that.getList()
              that.$message.success(
                '操作成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    changeDate(dates,dateStr){
      this.queryParam.purchaseDate = dateStr
    },
    statusFormat (row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    handlePay(record) {
      this.showPayModal = true
      this.$nextTick(() => (
        this.$refs.bizPurchasePayForm.handlePay(record)
      ))
    },
    // 仓库字典翻译
    warehouseIdFormat(row) {
      if (row.warehouseId) {
        return this.selectDictLabel(this.warehouseIdOptions, row.warehouseId)
      } else {
        return ''
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        purchaseDate: undefined,
        supplierCode: undefined,
        status: undefined

      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleAdd(record) {
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizPurchaseAddForm.handleAdd(record)
      ))
    },
    handleUpdate(record, ids) {
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizPurchaseAddForm.handleAdd(record)
      ))
    },
    handleView(record){
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizPurchaseAddForm.handleView(record)
      ))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk() {
          return delBizPurchase(row.id)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
            })
        },
        onCancel() { }
      })
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },
    
    customRow (record) {
        return {
          on: {
            dblclick: () => {
              this.$nextTick(() => this.handleView(record))
            }
          }
        }
      }
  }
}
</script>